/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress;

import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import io.jboot.Jboot;
import io.jboot.app.config.annotation.ConfigModel;
import io.jboot.utils.StrUtil;

import java.io.File;

/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 * @Title: JPress 环境配置
 * @Package io.jpress
 */
@ConfigModel(prefix = "jpress.core")
public class JPressConfig {

    public static final String DEFAULT_LOGIN_PAGE = "/admin/login";

    private String indexAction = "/page";
    private String defaultTemplate = "cn.jeanstudio.lightlog";

    private String attachmentRoot; // attachment 目录，在未配置的情况下，在 webapp 目录下
    private boolean attachmentDomainEnable = false; // 是否使用绝对路径

    // 绝对路径，可以配合 aliyunOssContainer 或者 minioContainer 来做绝对路径返回，
    // 这样当在 docker k8s 场景下，动态部署的容器里，可以不用带有附件，因为附件直接去 container 源地址访问了
    // 同时，可以保证 CDN 可以和附件地址不同，或者可以不开启 CDN
    private String attachmentDomain;


    private String adminLoginPage = DEFAULT_LOGIN_PAGE;         //登录的页面
    private String adminLoginAction = "/admin/doLogin";     //登录的方法
    private boolean adminLoginCaptchaValidateEnable = true; //后台登录是否使用验证码进行验证

    private String addonRoot; //插件安装的目录，方便在插件安装后，进行 clear 无效重新安装

    private String templateRoot; //模板的安装路径，方便在集群下挂载到此目录下

    private String defaultEditor = "ckeditor5";
//    private String defaultEditor = "ckeditor4";
//    private String defaultEditor = "wangEditor";

    /**
     * 前端加密秘钥（公钥）
     */
    private String frontPublicKey;

    /**
     * 后端解密秘钥（私钥）
     */
    private String frontPrivateKey;

    private boolean viewsCountWriteRealTimeEnable = false;
    private boolean utmWriteRealTimeEnable = false;


    public String getIndexAction() {
        return indexAction;
    }

    public void setIndexAction(String indexAction) {
        this.indexAction = indexAction;
    }

    public String getDefaultTemplate() {
        return defaultTemplate;
    }

    public void setDefaultTemplate(String defaultTemplate) {
        this.defaultTemplate = defaultTemplate;
    }


    public String getAttachmentRoot() {
        return attachmentRoot;
    }

    public void setAttachmentRoot(String attachmentRoot) {
        if (attachmentRoot != null && attachmentRoot.endsWith(File.separator)) {
            attachmentRoot = attachmentRoot.substring(0, attachmentRoot.length() - 1);
        }
        this.attachmentRoot = attachmentRoot;
    }

    public String getAdminLoginPage() {
        return adminLoginPage;
    }

    public void setAdminLoginPage(String adminLoginPage) {
        this.adminLoginPage = adminLoginPage;
    }

    public String getAdminLoginAction() {
        return adminLoginAction;
    }

    public void setAdminLoginAction(String adminLoginAction) {
        this.adminLoginAction = adminLoginAction;
    }

    public boolean isAdminLoginCaptchaValidateEnable() {
        return adminLoginCaptchaValidateEnable;
    }

    public void setAdminLoginCaptchaValidateEnable(boolean adminLoginCaptchaValidateEnable) {
        this.adminLoginCaptchaValidateEnable = adminLoginCaptchaValidateEnable;
    }

    public String getAttachmentRootOrWebRoot() {
        return StrUtil.isNotBlank(attachmentRoot)
                ? attachmentRoot
                : PathKit.getWebRootPath();
    }

    public boolean isAttachmentDomainEnable() {
        return attachmentDomainEnable;
    }

    public void setAttachmentDomainEnable(boolean attachmentDomainEnable) {
        this.attachmentDomainEnable = attachmentDomainEnable;
    }

    public String getAttachmentDomainOrContextPath() {
        if (!attachmentDomainEnable) {
            return JFinal.me().getContextPath();
        }
        return StrUtil.isBlank(attachmentDomain) ? JFinal.me().getContextPath() : attachmentDomain;
    }

    public void setAttachmentDomain(String attachmentDomain) {
        this.attachmentDomain = attachmentDomain;
    }

    public String getAddonRoot() {
        return StrUtil.isNotBlank(addonRoot) ? addonRoot : PathKit.getWebRootPath();
    }

    public void setAddonRoot(String addonRoot) {
        if (addonRoot != null && addonRoot.endsWith(File.separator)) {
            addonRoot = addonRoot.substring(0, addonRoot.length() - 1);
        }
        this.addonRoot = addonRoot;
    }

    public String getTemplateRoot() {
        return StrUtil.isNotBlank(templateRoot) ? templateRoot : PathKit.getWebRootPath();
    }

    public void setTemplateRoot(String templateRoot) {
        if (templateRoot != null && templateRoot.endsWith(File.separator)) {
            templateRoot = templateRoot.substring(0, templateRoot.length() - 1);
        }
        this.templateRoot = templateRoot;
    }

    public String getDefaultEditor() {
        return defaultEditor;
    }

    public void setDefaultEditor(String defaultEditor) {
        this.defaultEditor = defaultEditor;
    }

    public String getFrontPublicKey() {
        return frontPublicKey;
    }

    public void setFrontPublicKey(String frontPublicKey) {
        this.frontPublicKey = frontPublicKey;
    }

    public String getFrontPrivateKey() {
        return frontPrivateKey;
    }

    public void setFrontPrivateKey(String frontPrivateKey) {
        this.frontPrivateKey = frontPrivateKey;
    }

    public boolean isViewsCountWriteRealTimeEnable() {
        return viewsCountWriteRealTimeEnable;
    }

    public void setViewsCountWriteRealTimeEnable(boolean viewsCountWriteRealTimeEnable) {
        this.viewsCountWriteRealTimeEnable = viewsCountWriteRealTimeEnable;
    }

    public boolean isUtmWriteRealTimeEnable() {
        return utmWriteRealTimeEnable;
    }

    public void setUtmWriteRealTimeEnable(boolean utmWriteRealTimeEnable) {
        this.utmWriteRealTimeEnable = utmWriteRealTimeEnable;
    }

    public static final JPressConfig me = Jboot.config(JPressConfig.class);


}
